﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Spider
{
    public class MonitorManager
    {
        private Util mutil;

        public MonitorManager(Util util)
        {
            mutil = util;
        }

        public void Process()
        {
            int queryuriCount;
            int prouriCount;
            while (true)
            {
                queryuriCount=0;
                prouriCount=0;

                //侦听线程数
                ThreadCountListener();

                //显示rules信息
                mutil.InfoForm.ShowCurrentRuleInfo(mutil.ProductUrlCount, mutil.WaitProductUrlCount, mutil.UrlCount, mutil.WaitUrlCount);

                #region 判断当前rule是否结束
                foreach (CrawWorker cw in mutil.clist)
                {
                    if (cw.QueryURI == null)
                        queryuriCount++;
                }
                foreach (ScrapWorker sw in mutil.slist)
                {
                    if (sw.ProURI == null)
                        prouriCount++;
                }
                if (queryuriCount >= mutil.clist.Count && prouriCount >= mutil.slist.Count)
                {
                    //当前rule结束后执行操作
                    UpdataRules();
                }
                #endregion

                if (CheckStop())
                {
                    mutil.SaveInfos();
                    break;
                }
                Thread.Sleep(100);
            }
        }

        private void UpdataRules()
        {
            bool isFinish = true;
            Rules rule = DataHandler.Instance.CurrentRule;
            rule.Status = Convert.ToInt16(Status.STATUS_SUCCESS);
            Rules.Instance.ModifyRuleToXMLNotReload(rule);
            mutil.isFinishSpider = true;
            mutil.SaveInfos();
            foreach (Rules r in Rules.Instance.DicRules.Values)
            {
                if (r.Status.Equals(Convert.ToInt16(Spider.Status.STATUS_UNQUEUEDING)))
                {
                    r.Status = Convert.ToInt16(Status.STATUS_QUEUEDING);
                    DataHandler.Instance.ResetCurRule();
                    isFinish = false;
                    break;
                }
            }
            if (isFinish)
            {
                //所有都结束并重新循环
                mutil.isFinishSpider = false;
                mutil.clear();
                DataHandler.Instance.ResetCurRule();
                mutil.defaultOper();
            }
            else
            {
                //当前rule结束，且有其他rule需要执行
                mutil.isFinishSpider = false;
                mutil.clear();
                mutil.defaultOper();
            }
        }

        private void ThreadCountListener()
        {
            int threadcount = 0;
            foreach (CrawWorker cw in mutil.clist)
            {
                if (cw.MThread == null)
                    continue;

                if (cw.MThread.ThreadState != ThreadState.Stopped)
                    threadcount++;
            }
            foreach (ScrapWorker sw in mutil.slist)
            {
                if (sw.MThread == null)
                    continue;

                if (sw.MThread.ThreadState != ThreadState.Stopped)
                    threadcount++;
            }
            mutil.done.SetThreadsCount(threadcount);
            mutil.InfoForm.showThreadNum(threadcount);
        }

        public bool CheckStop()
        {
            if(mutil.done.ActiveThreads>0)
                return false;
            else
                return true;
        }

        public bool CheckFinish()
        {
            return false;
        }

        public void Start()
        {
            Thread newThread = new Thread(Process);
            newThread.Start();
        }
    }
}
